home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.06 Jun 89 / SprayCan Source / ToolDemo.Pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-11-19  |  8.6 KB  |  355 lines  |  [TEXT/EDIT]

  1. Program ToolDemo;
  2.  
  3. { Turbo Pascal format }
  4.  
  5. {$U-}
  6. {$R ToolDemoRes}
  7.  
  8. Uses
  9.   MemTypes,QuickDraw,OSIntf,ToolIntf;
  10.   
  11. Const
  12.   AppleMenu     = 128;
  13.   FileMenu      = 129;
  14.   EditMenu      = 130;
  15.   ToolMenu      = 131;
  16.     SprayItem   = 1;
  17.     BucketItem  = 2;
  18.   PatternMenu   = 132;
  19.     WhiteItem   = 1;
  20.     LtGrayItem  = 2;
  21.     GrayItem    = 3;
  22.     DkGrayItem  = 4;
  23.     BlackItem   = 5;
  24.   
  25. Var
  26.   myMenus       : array [AppleMenu..PatternMenu] of MenuHandle;
  27.   myWindow      : WindowPtr;
  28.   Finished      : Boolean;
  29.   GrowArea      : Rect;
  30.   CurrentPat    : Integer;
  31.   CurrentTool   : Integer;
  32.  
  33. {###############################################################################}
  34.  
  35. Function NewBitMap (VAR theBitMap : BitMap; theRect : Rect) : Ptr;
  36. Begin
  37.   NewBitMap:= nil;
  38.   with theBitMap,theRect do begin
  39.     rowBytes:= ((right-left+15) DIV 16) *2;
  40.     baseAddr:= NewPtr(rowBytes * (bottom-top));
  41.     bounds:= theRect;
  42.     if MemError = noErr then
  43.       NewBitMap:= baseAddr;
  44.   end;
  45. End;
  46.  
  47. Procedure DoSprayCan (where : Point);
  48. Var
  49.   workPort  : GrafPtr;
  50.   workBits  : BitMap;
  51.   workRect  : Rect;
  52.   workPat   : Pattern;
  53.   theStr    : Str255;
  54.   tempRect  : Rect;
  55.   SprayBits : BitMap;
  56.   PatBits   : BitMap;
  57.   tickValue : LongInt;
  58. Begin
  59.   GetPort(workPort);
  60.   workBits:= workPort^.portBits;
  61.   workRect:= workPort^.portRect;
  62.   workPat:= workPort^.pnPat;
  63.  
  64.   GetIndString(theStr,128,1);
  65.   SetRect(tempRect,0,0,16,16);
  66.   if NewBitMap(SprayBits,tempRect) = nil then Exit;
  67.   StuffHex(SprayBits.baseAddr,theStr);
  68.  
  69.   if NewBitMap(PatBits,workRect) = nil then begin
  70.     DisposPtr(SprayBits.baseAddr);
  71.     Exit;
  72.   end;
  73.   SetPortBits(PatBits);
  74.   FillRect(workRect,workPat);
  75.   SetPortBits(workBits);
  76.  
  77.   repeat
  78.     GetMouse(where);
  79.     with where do
  80.       SetRect(tempRect,h-8,v-8,h+8,v+8);
  81.     tickValue:= TickCount + 1;
  82.     repeat until (tickValue <= TickCount);
  83.     CopyMask(PatBits,SprayBits,workBits,tempRect,SprayBits.bounds,tempRect);
  84.     tickValue:= TickCount;
  85.     repeat until (tickValue <= TickCount);
  86.   until NOT Button;
  87.   
  88.   DisposPtr(PatBits.baseAddr);
  89.   DisposPtr(SprayBits.baseAddr);
  90. End;
  91.  
  92. Procedure DoPaintBucket (where : Point);
  93. Var
  94.   workPort  : GrafPtr;
  95.   workBits  : BitMap;
  96.   workRect  : Rect;
  97.   workPat   : Pattern;
  98.   PatBits   : BitMap;
  99.   onBlack   : Boolean;
  100.   srcMap    : BitMap;
  101.   dstMap    : BitMap;
  102.   srcPtr    : Ptr;
  103.   dstPtr    : Ptr;
  104.   srcRow    : Integer;
  105.   dstRow    : Integer;
  106.   height    : Integer;
  107.   words     : Integer;
  108. Begin
  109.   GetPort(workPort);
  110.   workBits:= workPort^.portBits;
  111.   workRect:= workPort^.portRect;
  112.   workPat:= workPort^.pnPat;
  113.   
  114.   if NewBitMap(dstMap,workRect) = nil then Exit;
  115.   if NewBitMap(srcMap,workRect) = nil then begin
  116.     DisposPtr(dstMap.baseAddr);
  117.     Exit;
  118.   end;
  119.  
  120.   CopyBits(workBits,srcMap,workRect,workRect,srcCopy,nil);
  121.  
  122.   onBlack:= GetPixel(where.h,where.v);
  123.   if onBlack then begin
  124.     SetPortBits(srcMap);
  125.     InvertRect(workRect);
  126.     SetPortBits(workBits);
  127.   end;
  128.   
  129.   if NewBitMap(PatBits,workRect) = nil then begin
  130.     DisposPtr(dstMap.baseAddr);
  131.     DisposPtr(srcMap.baseAddr);
  132.     Exit;
  133.   end;
  134.   SetPortBits(PatBits);
  135.   FillRect(workRect,workPat);
  136.   if onBlack then InvertRect(workRect);
  137.   SetPortBits(workBits);
  138.   
  139.   srcPtr:= srcMap.baseAddr;
  140.   srcRow:= srcMap.rowBytes;
  141.  
  142.   dstPtr:= dstMap.baseAddr;
  143.   dstRow:= dstMap.rowBytes;
  144.  
  145.   height:= dstMap.bounds.bottom - dstMap.bounds.top;
  146.   words:= (dstRow + 1) DIV 2;
  147.  
  148.   SeedFill(srcPtr,dstPtr,srcRow,dstRow,height,words,where.h,where.v);
  149.   CopyMask(PatBits,dstMap,srcMap,workRect,workRect,srcMap.bounds);
  150.   
  151.   if onBlack then begin
  152.     SetPortBits(srcMap);
  153.     InvertRect(workRect);
  154.     SetPortBits(workBits);
  155.   end;
  156.   CopyBits(srcMap,workBits,workRect,workRect,srcCopy,nil);
  157.  
  158.   DisposPtr(srcMap.baseAddr);
  159.   DisposPtr(dstMap.baseAddr);
  160.   DisposPtr(PatBits.baseAddr);
  161. End;
  162.  
  163. {###############################################################################}
  164.  
  165. Procedure ProcessMenu (codeWord : LongInt);
  166. Var
  167.   menuNum   : Integer;
  168.   itemNum   : Integer;
  169.   itemStr   : Str255;
  170.   dummy     : Integer;
  171. Begin
  172.   if codeWord <> 0 then begin
  173.     menuNum := HiWord(codeWord);
  174.     itemNum := LoWord(codeWord);
  175.     case menuNum of
  176.       AppleMenu :
  177.         begin
  178.           GetItem(myMenus[AppleMenu],itemNum,itemStr);
  179.           dummy:= OpenDeskAcc(itemStr);
  180.         end;
  181.       FileMenu : Finished:= TRUE;
  182.       EditMenu : if NOT SystemEdit(itemNum - 1) then ;
  183.       ToolMenu :
  184.         begin
  185.           CheckItem(myMenus[ToolMenu],CurrentTool,false);
  186.           CurrentTool:= itemNum;
  187.           CheckItem(myMenus[ToolMenu],CurrentTool,true);
  188.         end;
  189.       PatternMenu :
  190.         begin
  191.           CheckItem(myMenus[PatternMenu],CurrentPat,false);
  192.           CurrentPat:= itemNum;
  193.           CheckItem(myMenus[PatternMenu],CurrentPat,true);
  194.           SetPort(myWindow);
  195.           case CurrentPat of
  196.             WhiteItem   : PenPat(white);
  197.             LtGrayItem  : PenPat(ltGray);
  198.             GrayItem    : PenPat(gray);
  199.             DkGrayItem  : PenPat(dkGray);
  200.             BlackItem   : PenPat(black);
  201.           end
  202.         end;
  203.     end; {case}
  204.     HiliteMenu(0);
  205.   end; {big if}
  206. End;
  207.  
  208. {###############################################################################}
  209.  
  210. Procedure DealWithMouseDowns(theEvent: EventRecord);
  211. Var
  212.   whichWindow   : WindowPtr;
  213.   mouseLoc      : Point;
  214.   windowLoc     : Integer;
  215.   position      : LongInt;
  216. Begin
  217.   mouseLoc:= theEvent.where;
  218.   windowLoc:= FindWindow(mouseLoc,whichWindow);
  219.   case windowLoc of
  220.     inMenuBar   : ProcessMenu(MenuSelect(mouseLoc));
  221.     inSysWindow : SystemClick(theEvent,whichWindow);
  222.     inDrag      : DragWindow(whichWindow,mouseLoc,screenBits.bounds);
  223.     inGoAway    : if TrackGoAway(whichWindow,mouseLoc) then Finished:= true;
  224.     inGrow :
  225.       begin
  226.         position:= GrowWindow(whichWindow,mouseLoc,GrowArea);
  227.         if position <> 0 then begin
  228.           SizeWindow(whichWindow,loword(position),hiword(position),false);
  229.           SetPort(whichWindow);
  230.           InvalRect(whichWindow^.portRect);
  231.         end;
  232.       end;
  233.     inZoomIn,inZoomOut :
  234.       begin
  235.         if TrackBox(whichWindow,mouseLoc,windowLoc) then begin
  236.           SetPort(whichWindow);
  237.           ClipRect(whichWindow^.portRect);
  238.           EraseRect(whichWindow^.portRect);
  239.           ZoomWindow(whichWindow,windowLoc,true);
  240.           InvalRect(whichWindow^.portRect);
  241.         end;
  242.       end;
  243.     inContent :
  244.       begin
  245.         if whichWindow <> FrontWindow then
  246.           SelectWindow(whichWindow)
  247.         else begin
  248.           SetPort(whichWindow);
  249.           GlobalToLocal(mouseLoc);
  250.           case CurrentTool of
  251.             SprayItem   : DoSprayCan(mouseLoc);
  252.             BucketItem  : DoPaintBucket(mouseLoc);
  253.           end;
  254.         end;
  255.       end;
  256.   end;
  257. End;
  258.  
  259. Procedure DealWithKeyDowns(theEvent: EventRecord);
  260. Var
  261.   CharCode  : char;
  262. Begin
  263.   CharCode:= CHR(BitAnd(theEvent.message,charCodeMask));
  264.   if BitAnd(theEvent.modifiers,CmdKey) = CmdKey
  265.     then ProcessMenu(MenuKey(CharCode));
  266. End;
  267.  
  268. Procedure DealWithActivates(theEvent: EventRecord);
  269. Var
  270.   theWindow    : WindowPtr;
  271. Begin
  272.   theWindow := WindowPtr(theEvent.message);
  273.   if Odd(theEvent.modifiers)
  274.     then SetPort(theWindow);
  275. End;
  276.  
  277. Procedure DealWithUpdates(theEvent: EventRecord);
  278. Var
  279.   theWindow : WindowPtr;
  280.   tempPort  : WindowPtr;
  281. Begin
  282.   theWindow := WindowPtr(theEvent.message);
  283.   GetPort(tempPort);
  284.   SetPort(theWindow); 
  285.   BeginUpDate(theWindow);
  286.     ClipRect(theWindow^.portRect);
  287.     EraseRect(theWindow^.portRect);
  288.     PenSize(5,5);
  289.     FrameOval(theWindow^.portRect);
  290.     PenSize(1,1);
  291.   EndUpDate(theWindow);
  292.   SetPort(tempPort);
  293. End;
  294.  
  295. Procedure MainEventLoop;
  296. Var
  297.   Event        : EventRecord;
  298. Begin
  299.   repeat
  300.     SystemTask;
  301.     if GetNextEvent(everyEvent, Event) then
  302.       case Event.what of
  303.         mouseDown   : DealWithMouseDowns(Event);
  304.         AutoKey     : DealWithKeyDowns(Event);
  305.         KeyDown     : DealWithKeyDowns(Event);
  306.         ActivateEvt : DealWithActivates(Event);
  307.         UpdateEvt   : DealWithUpdates(Event);
  308.       end; {case}
  309.   until Finished;
  310. End;
  311.  
  312. {###############################################################################}
  313.  
  314. Procedure SetupStuff;
  315. Var
  316.   index : Integer;
  317. Begin
  318.   MaxApplZone;
  319.   InitGraf(@thePort);
  320.   InitFonts;
  321.   InitWindows;
  322.   InitMenus;
  323.   TEInit;
  324.   InitDialogs(nil);
  325.  
  326.   for index:= AppleMenu to PatternMenu do begin
  327.     myMenus[index] := GetMenu(index);
  328.     InsertMenu(myMenus[index],0);
  329.   end;
  330.   AddResMenu(myMenus[AppleMenu],'DRVR');
  331.   DrawMenuBar;
  332.  
  333.   CurrentTool:= SprayItem;
  334.   CurrentPat:= BlackItem;
  335.   
  336.   CheckItem(myMenus[ToolMenu],CurrentTool,true);
  337.   CheckItem(myMenus[PatternMenu],CurrentPat,true);
  338.  
  339.   myWindow:= GetNewWindow(1000,nil,pointer(-1));
  340.   
  341.   Finished:= false;
  342.   with screenBits.bounds do
  343.     SetRect(GrowArea,150,150,right,bottom);
  344.  
  345.   FlushEvents(everyEvent,0);
  346.  
  347.   InitCursor;
  348. End;
  349.  
  350. Begin
  351.   SetupStuff;
  352.   
  353.   MainEventLoop;
  354. End.
  355.